package com.buddy.learn.producer;

import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author buddy
 */
@Service
@Slf4j
@ConditionalOnProperty(prefix = "rocketmq", value = {"name-server", "producer.group"})
public class RocketMqRepository {

    @Resource(name = "rocketMQTemplate")
    private RocketMQTemplate rocketMQTemplate;

    public <T> void sendMessage(String message, String topic) {
        try {
            SendResult sendResult = rocketMQTemplate.syncSend(topic, message);
            log.debug("RocketMqRepository sendNotifyMessage sendResult={}", message);
        } catch (Exception e) {
            log.error("RocketMqRepository sendNotifyMessage error,message={}", message,e);
        }
    }
}
